import sys
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5 import QtCore
import PyQt5
import requests
import window
import os
import traceback
import argparse
from modules import *

allModules = [["http", "Scan for open HTTP ports, and get the titles."],
              ["mongodb", "Scan for open MongoDB instances, and check if they are password protected."],
              ["mysql", "Scan for open MySQL servers, and try to log in with the default credentials."],
              ["ssh", "Scan for open SSH ports."], ["printer", "Scan for open printer ports and websites."],
              ["gameserver", "Scan for open game server ports."],
              ["manual", "Scan custom ports."], ["template", "Template module for developers."]]

textToModule = [["HTTP", http],  ["Printer", printer], ["GameServer", gameserver], ["SSH", ssh], ["Manual", manual], ["MongoDB", mongodb], ["MySQL", mysql]]


def bindslot():
    ui.comboBox.currentTextChanged.connect(moduleChange)
    ui.pushButton.clicked.connect(run)


def moduleChange():
    if ui.comboBox.currentText() == 'MySQL':
        ui.modulesdis.setText('Scan for open MySQL servers, and try to log in with the default credentials.')

        ui.network.setEnabled(True)
        ui.port.setEnabled(True)
        ui.port.setText('3306')
        ui.porttimeout.setEnabled(True)
        ui.porttimeout.setText('0.3')
        ui.dbtimeout.setEnabled(True)
        ui.dbtimeout.setText('3')
        ui.checkauth.setEnabled(True)
        ui.checkauth.setText('true')
        ui.threads.setEnabled(True)
        ui.threads.setText('50')
        ui.titletimeout.setEnabled(False)
        ui.ports.setEnabled(False)
        ui.verbose.setEnabled(True)
        ui.verbose.setText('true')

    elif ui.comboBox.currentText() == 'MongoDB':
        ui.modulesdis.setText('Scan for open MongoDB instances, and check if they are password protected.')

        ui.network.setEnabled(True)
        ui.port.setEnabled(True)
        ui.port.setText('27017')
        ui.porttimeout.setEnabled(True)
        ui.porttimeout.setText('0.3')
        ui.dbtimeout.setEnabled(True)
        ui.dbtimeout.setText('3')
        ui.checkauth.setEnabled(True)
        ui.checkauth.setText('true')
        ui.threads.setEnabled(True)
        ui.threads.setText('50')
        ui.titletimeout.setEnabled(False)
        ui.ports.setEnabled(False)
        ui.verbose.setEnabled(True)
        ui.verbose.setText('true')

    elif ui.comboBox.currentText() == 'SSH':
        ui.modulesdis.setText('Scan for open SSH ports.')

        ui.network.setEnabled(True)
        ui.port.setEnabled(False)
        ui.porttimeout.setEnabled(True)
        ui.porttimeout.setText('0.3')
        ui.dbtimeout.setEnabled(False)
        ui.checkauth.setEnabled(False)
        ui.threads.setEnabled(True)
        ui.threads.setText('50')
        ui.titletimeout.setEnabled(False)
        ui.ports.setEnabled(False)
        ui.verbose.setEnabled(True)
        ui.verbose.setText('true')

    elif ui.comboBox.currentText() == 'HTTP':
        ui.modulesdis.setText('Scan for open HTTP ports, and get the titles.')

        ui.network.setEnabled(True)
        ui.port.setEnabled(False)
        ui.porttimeout.setEnabled(True)
        # ui.porttimeout.setEnabled('true')
        ui.dbtimeout.setEnabled(False)
        ui.checkauth.setEnabled(False)
        ui.threads.setEnabled(True)
        # ui.threads.setText('50')
        ui.titletimeout.setEnabled(True)
        # ui.titletimeout.setText('3')
        ui.ports.setEnabled(False)
        ui.verbose.setEnabled(True)
        # ui.verbose.setText('true')

    elif ui.comboBox.currentText() == 'Printer':
        ui.modulesdis.setText('Scan for open printer ports and websites.')

        ui.network.setEnabled(True)
        ui.port.setEnabled(False)
        ui.porttimeout.setEnabled(True)
        ui.porttimeout.setText('0.3')
        ui.dbtimeout.setEnabled(False)
        ui.checkauth.setEnabled(False)
        ui.threads.setEnabled(True)
        ui.threads.setText('50')
        ui.titletimeout.setEnabled(True)
        ui.titletimeout.setText('3')
        ui.ports.setEnabled(False)
        ui.verbose.setEnabled(True)
        ui.verbose.setText('true')

    elif ui.comboBox.currentText() == 'GameServer':
        ui.modulesdis.setText('Scan for open game server ports.')
        ui.network.setEnabled(True)
        ui.port.setEnabled(False)
        ui.porttimeout.setEnabled(True)

        ui.dbtimeout.setEnabled(False)
        ui.checkauth.setEnabled(False)
        ui.threads.setEnabled(True)
        ui.titletimeout.setEnabled(False)
        ui.ports.setEnabled(False)
        ui.verbose.setEnabled(True)

    elif ui.comboBox.currentText() == 'Manual':
        ui.modulesdis.setText('Scan custom ports.')

        ui.network.setEnabled(True)
        ui.port.setEnabled(False)
        ui.porttimeout.setEnabled(True)
        ui.dbtimeout.setEnabled(False)
        ui.checkauth.setEnabled(False)
        ui.threads.setEnabled(True)
        ui.titletimeout.setEnabled(False)
        ui.ports.setEnabled(True)
        ui.verbose.setEnabled(True)

def getOptionsbyName(OptionName):
    if OptionName == 'network':
        return ui.network.text()
    elif OptionName == 'port':
        return ui.port.text()
    elif OptionName == 'ports':
        return ui.ports.text()
    elif OptionName == 'port-timeout':
        return ui.porttimeout.text()
    elif OptionName == 'threads':
        return ui.threads.text()
    elif OptionName == 'verbose':
        return ui.verbose.text()
    elif OptionName == 'sql-timeout':
        return ui.dbtimeout.text()
    elif OptionName == 'mongo-timeout':
        return ui.dbtimeout.text()
    elif OptionName == 'title-timeout':
        return ui.titletimeout.text()
    elif OptionName == 'checkauth':
        return ui.checkauth.text()

def run():
    CurrentModule = None
    for module in textToModule:
        if module[0] == ui.comboBox.currentText():
            CurrentModule = module[1]
    getCoreOptions = getattr(CurrentModule, "coreOptions", None)
    moduleOptions = getCoreOptions()
    #更新option
    for i, option in enumerate(moduleOptions):
        moduleOptions[i][2] = getOptionsbyName(option[0])
    coreModule = getattr(CurrentModule, "core")
    coreModule(moduleOptions, ui)
    # if ui.comboBox.currentText() == 'MySQL':
    #     pass
    # elif ui.comboBox.currentText() == 'MongoDB':
    #     pass
    # elif ui.comboBox.currentText() == 'SSH':
    #     pass
    # elif ui.comboBox.currentText() == 'HTTP':
    #     pass
    # elif ui.comboBox.currentText() == 'Printer':
    #     pass
    # elif ui.comboBox.currentText() == 'GameServer':
    #     pass
    # elif ui.comboBox.currentText() == 'Manual':
    #     pass
if __name__ == '__main__':
    app = PyQt5.QtWidgets.QApplication(sys.argv)
    mainWindow = PyQt5.QtWidgets.QMainWindow()
    ui = window.Ui_MainWindow()
    ui.setupUi(mainWindow)
    bindslot()
    mainWindow.show()
    header = r"""
███████╗███████╗███████╗ ██████╗ █████╗ ███╗   ██╗███╗   ██╗███████╗██████╗ 
██╔════╝╚══███╔╝██╔════╝██╔════╝██╔══██╗████╗  ██║████╗  ██║██╔════╝██╔══██╗
█████╗    ███╔╝ ███████╗██║     ███████║██╔██╗ ██║██╔██╗ ██║█████╗  ██████╔╝
██╔══╝   ███╔╝  ╚════██║██║     ██╔══██║██║╚██╗██║██║╚██╗██║██╔══╝  ██╔══██╗
███████╗███████╗███████║╚██████╗██║  ██║██║ ╚████║██║ ╚████║███████╗██║  ██║
╚══════╝╚══════╝╚══════╝ ╚═════╝╚═╝  ╚═╝╚═╝  ╚═══╝╚═╝  ╚═══╝╚══════╝╚═╝  ╚═╝   
"""

    print(header + "                         v1.0 by Luyi \n")
    sys.exit(app.exec_())